import React from "react";


//hocStateExtract 函数叫高阶组件 : 本质上是一个函数
//返回的组件 就是一个组件

export default function hocStateExtract(WrapperComponent) {
  return class extends React.Component {
    state = {
      time: new Date().toLocaleTimeString(),
    };
    timer = null;
    constructor(props) {
      super(props);
    }
    componentDidMount() {
      this.start();
    }
    start = () => {
      this.timer = setInterval(() => {
        this.setState({
          time: new Date().toLocaleTimeString()
        })
      }, 1000);
    };
    stopTime = () => {
      clearInterval(this.timer);
    };

    startTime = () => {
      this.start();
    };
    componentWillUnmount() {
      clearInterval(this.timer);
    }
    render() {
      const timeObj = {
        time: this.state.time,
        stopTime: this.stopTime,
        startTime:this.startTime
      };
      return (
        <div>
          <WrapperComponent {...this.props} {...timeObj}/>
        </div>
      );
    }
  }
};
